package com.mz361.stdapi.config.properties;//package com.mz361.api.config.properties;


import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
@Slf4j
public class ESConfig {
    @Value("${project.es.host}")
    private String host;
    @Value("${project.es.port}")
    private Integer port;
    @Value("${project.es.scheme}")
    private String scheme;

    @Bean
    @Primary
    public RestHighLevelClient restHighLevelClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, scheme));
        builder.setHttpClientConfigCallback(requestConfigBuilder ->
                requestConfigBuilder.setKeepAliveStrategy((response, context) -> 1000 * 60 * 3));
        RestHighLevelClient client = new RestHighLevelClient(builder);
        try {
            UpdateSettingsRequest request = new UpdateSettingsRequest("standard_info");
            Settings settings = Settings.builder()
                    .put("index.max_result_window", 50000) // 设置为你需要的最大值
                    .build();
            request.settings(settings);
            client.indices().putSettings(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("修改索引设置失败", e);
        }
        log.info("ElasticSearch connection successful host:{} port:{}", host, port);
        return client;

    }

}
